package com.xhwl.centiotdata.suppliersystem.eesd.qiming;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.constant.EesdConstant;
import com.xhwl.centiotdata.utils.LocalDateTimeUtils;
import com.xhwl.common.dto.cent.bi.OdsChargeMetricsDTO;
import com.xhwl.common.dto.cent.iotdata.ev.*;
import com.xhwl.common.pojo.cent.device.DeviceProjSupplierCategory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author： lianghz
 * @Description：
 * @Date 2021/09/03/ 09:44
 */
@Slf4j
@RestController
public class QiMingApiHandler {

    @Resource
    private QiMingUtil qiMingUtil;

    /**
     * 充电桩相关信息
     * @return
     */
    public List<OdsChargeMetricsDTO> pullStationInfo(DeviceProjSupplierCategory config) {
        Integer projectId = config.getProjectId();
        Integer productId = config.getProductId();
        JSONObject properties = config.getProperties();
        ChargeConfigDTO dto = JSONObject.parseObject(JSON.toJSONString(properties), ChargeConfigDTO.class);

        String s = qiMingUtil.queryStationsInfo(dto);
        JSONObject respObj = JSONObject.parseObject(s);
        String stationInfos = respObj.getString("StationInfos");

        List<ChargeStationRespDTO> stationResp = JSONObject.parseArray(stationInfos, ChargeStationRespDTO.class);

        List<OdsChargeMetricsDTO> list = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        for (ChargeStationRespDTO chargeStationRespDTO : stationResp) {
            String stationID = chargeStationRespDTO.getStationID();
            String stationName = chargeStationRespDTO.getStationName();

            String equipmentInfos = chargeStationRespDTO.getEquipmentInfos();
            List<ChargeEquipmentRespDTO> equipmentResp = JSONObject.parseArray(equipmentInfos, ChargeEquipmentRespDTO.class);
            for (ChargeEquipmentRespDTO chargeEquipmentRespDTO : equipmentResp) {
                String equipmentID = chargeEquipmentRespDTO.getEquipmentID();
                String equipmentName = chargeEquipmentRespDTO.getEquipmentName();
                Integer type = chargeEquipmentRespDTO.getEquipmentType();
                Integer equipmentStatus = chargeEquipmentRespDTO.getEquipmentStatus();
                // 设备总功率
                BigDecimal power = chargeEquipmentRespDTO.getPower();

                String connectorInfos = chargeEquipmentRespDTO.getConnectorInfos();
                List<ChargeConnectorRespDTO> connectorResp = JSONObject.parseArray(connectorInfos, ChargeConnectorRespDTO.class);
                for (ChargeConnectorRespDTO chargeConnectorRespDTO : connectorResp) {
                    String connectorID = chargeConnectorRespDTO.getConnectorID();

                    OdsChargeMetricsDTO resp = new OdsChargeMetricsDTO();
                    // todo
                    resp.setProductId(productId);
                    resp.setProjectId(projectId);
                    // 南投的设备编码为11070508210827001的类型设置为6，为V2G类型
                    if (equipmentID.equals(EesdConstant.NANTOU_V2G)) {
                        resp.setType(EesdConstant.NUM_6);
                    } else {
                        resp.setType(type);
                    }
                    resp.setStationId(stationID);
                    resp.setStationName(stationName);
                    resp.setConnectorId(connectorID);
                    resp.setThirdDeviceId(equipmentID);
                    resp.setDeviceName(equipmentName);
                    resp.setDeviceStatus(equipmentStatus);
                    resp.setObsTime(now);
                    resp.setRatedPower(power);
                    list.add(resp);
                }
            }
        }
        log.info("充电桩数据: {}", JSONObject.toJSONString(list));
        return list;
    }

    /**
     * 充电桩状态
     * @return
     */
    public List<OdsChargeMetricsDTO> pullChargeStatus(DeviceProjSupplierCategory config) {
        Integer projectId = config.getProjectId();
        Integer productId = config.getProductId();
        JSONObject properties = config.getProperties();
        ChargeConfigDTO dto = JSONObject.parseObject(JSON.toJSONString(properties), ChargeConfigDTO.class);
        String s = qiMingUtil.queryStationsStatus(dto);
        JSONObject resp = JSONObject.parseObject(s);
        String stationStatusInfos = resp.getString("StationStatusInfos");

        List<ChargeStationRespDTO> respList = JSONObject.parseArray(stationStatusInfos, ChargeStationRespDTO.class);
        List<OdsChargeMetricsDTO> list = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        for (ChargeStationRespDTO chargeStationRespDTO : respList) {
            String stationID = chargeStationRespDTO.getStationID();
            String connectorStatusInfos = chargeStationRespDTO.getConnectorStatusInfos();
            List<ChargeConnectorStatusRespDTO> statusResp = JSONObject.parseArray(connectorStatusInfos, ChargeConnectorStatusRespDTO.class);
            for (ChargeConnectorStatusRespDTO chargeConnectorStatusRespDTO : statusResp) {
                OdsChargeMetricsDTO odsChargeMetricsDTO = new OdsChargeMetricsDTO();
                // todo
                odsChargeMetricsDTO.setProjectId(projectId);
                odsChargeMetricsDTO.setProductId(productId);

                odsChargeMetricsDTO.setStationId(stationID);
                odsChargeMetricsDTO.setConnectorId(chargeConnectorStatusRespDTO.getConnectorID());
                odsChargeMetricsDTO.setObsTime(now);
                Integer connectorStatus;
                switch (chargeConnectorStatusRespDTO.getStatus()) {
                    case 1:
                        connectorStatus = 1;
                        break;
                    case 3:
                        connectorStatus = 2;
                        break;
                    case 2:
                        connectorStatus = 3;
                        break;
                    case 255:
                        connectorStatus = 4;
                        break;
                    case 4:
                        connectorStatus = 6;
                        break;
                    default:
                        connectorStatus = 5;
                        break;
                }
                odsChargeMetricsDTO.setConnectorStatus(connectorStatus);
                list.add(odsChargeMetricsDTO);
            }
        }
        return list;
    }

    /**
     * 充电桩统计信息
     * @return
     */
    public List<OdsChargeMetricsDTO> pullChargeStats(DeviceProjSupplierCategory config) {
        Integer projectId = config.getProjectId();
        Integer productId = config.getProductId();
        JSONObject properties = config.getProperties();
        ChargeConfigDTO dto = JSONObject.parseObject(JSON.toJSONString(properties), ChargeConfigDTO.class);

        // 查询今日的充电量
        String nowStr = LocalDateTimeUtils.getYearMonthDay();
        String respStr = qiMingUtil.queryStationsStats(dto, nowStr, nowStr);
        List<OdsChargeMetricsDTO> list = new ArrayList<>();

        LocalDateTime now = LocalDateTime.now();
        JSONObject resp = JSONObject.parseObject(respStr);
        String stationInfos = resp.getString("StationStats");
        ChargeStatsRespDTO chargeStatsRespDTO = JSONObject.parseObject(stationInfos, ChargeStatsRespDTO.class);
        String stationID = chargeStatsRespDTO.getStationID();
        BigDecimal stationPower = chargeStatsRespDTO.getStationElectricity();
        String equipmentStatsInfos = chargeStatsRespDTO.getEquipmentStatsInfos();
        List<ChargeEquipmentStatsRespDTO> equipmentResp = JSONObject.parseArray(equipmentStatsInfos, ChargeEquipmentStatsRespDTO.class);
        for (ChargeEquipmentStatsRespDTO chargeEquipmentStatsRespDTO : equipmentResp) {
            String connectorStatsInfos = chargeEquipmentStatsRespDTO.getConnectorStatsInfos();
            String equipmentID = chargeEquipmentStatsRespDTO.getEquipmentID();
            BigDecimal devicePower = chargeEquipmentStatsRespDTO.getEquipmentElectricity();
            List<ChargeConnectorStatsRespDTO> connectorResp = JSONObject.parseArray(connectorStatsInfos, ChargeConnectorStatsRespDTO.class);
            for (ChargeConnectorStatsRespDTO chargeConnectorStatsRespDTO : connectorResp) {
                OdsChargeMetricsDTO odsChargeMetricsDTO = new OdsChargeMetricsDTO();
                // todo
                odsChargeMetricsDTO.setProductId(productId);
                odsChargeMetricsDTO.setProjectId(projectId);
                odsChargeMetricsDTO.setStationId(stationID);
                // 充电站累计电量
                odsChargeMetricsDTO.setStationPower(stationPower);
                odsChargeMetricsDTO.setThirdDeviceId(equipmentID);
                // 充电设备累计电量
                odsChargeMetricsDTO.setDevicePower(devicePower);
                odsChargeMetricsDTO.setConnectorId(chargeConnectorStatsRespDTO.getConnectorID());
                // 充电设备接口累计电量
                odsChargeMetricsDTO.setPower(chargeConnectorStatsRespDTO.getConnectorElectricity());
                odsChargeMetricsDTO.setObsTime(now);
                list.add(odsChargeMetricsDTO);
            }
        }
        return list;
    }


}
